package com.qdzsaas.dbwd.home.adapter

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import androidx.recyclerview.widget.RecyclerView
import com.qdzsaas.dbwd.R
import com.qdzsaas.dbwd.base.QdzApplication

/**
 *
 * @Description:     java类作用描述
 * @Author:         Melo
 * @CreateDate:     2021/5/7 0007 10:27
 * @UpdateUser:     更新者
 * @UpdateDate:     2021/5/7 0007 10:27
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */
class HorizontalScrollBarDecoration : RecyclerView.ItemDecoration() {

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)

        val barHeight =  QdzApplication.getContext().getResources().getDimension(R.dimen.dp_3)
        val scrollWidth = QdzApplication.getContext().getResources().getDimension(R.dimen.dp_25)
        val indicatorWidth = QdzApplication.getContext().getResources().getDimension(R.dimen.dp_13)
        val paddingBottom = QdzApplication.getContext().getResources().getDimension(R.dimen.dp_10)
        val barX = (parent.width / 2 - scrollWidth / 2).toFloat()
        val barY = (parent.height - paddingBottom - barHeight).toFloat()

        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = Color.parseColor("#8000AB39")
        paint.strokeCap = Paint.Cap.ROUND
        paint.strokeWidth = barHeight.toFloat()
        c.drawLine(barX, barY, barX + scrollWidth.toFloat(), barY, paint)

        val extent = parent.computeHorizontalScrollExtent()
        val range = parent.computeHorizontalScrollRange()
        val offset = parent.computeHorizontalScrollOffset()
        val maxEndX = (range - extent).toFloat()
        //可滑动
        if (maxEndX > 0) {
            val proportion = offset / maxEndX

            val scrollableDistance = scrollWidth - indicatorWidth

            val offsetX = scrollableDistance * proportion
            paint.color = Color.parseColor("#FF00AB39")
            c.drawLine(barX + offsetX, barY, barX + indicatorWidth.toFloat() + offsetX, barY, paint)
        } else {
            paint.color = Color.parseColor("#FF00AB39")
            c.drawLine(barX, barY, barX + scrollWidth.toFloat(), barY, paint)
        }
    }
}